package com.freejava.testredis.demo.controller;

import com.freejava.testredis.demo.pojo.Product;
import com.freejava.testredis.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

    @Autowired
    ProductService productService;

    // cacheable 注解，缓存的是方法的结果
    // 对象类型以什么形式保存到redis， 序列化 和 反序列化
    @RequestMapping("/products/{id}")
    @Cacheable(value = "userCache", key = "#root.targetClass.getName() + #root.methodName + #id")
    public Product findProductById(@PathVariable int id) {
        System.out.println("come here!");
        Long pid = Long.valueOf(id);
        Product product = productService.findOneById(pid);
        return product;
    }
}
